import { NotifyService } from '@abp/notify/notify.service';
import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core';
import { Http } from '@angular/http';
import { ActivatedRoute } from '@angular/router';
import { AppConsts } from '@shared/AppConsts';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { EquipmentServiceProxy, EquipmentListDto, ComboboxItemDto } from '@shared/service-proxies/service-proxies';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { CreateOrEditEquipmentModalComponent } from './create-or-edit-equipment-modal.component';


@Component({
  templateUrl: './equipments.component.html',
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()]
})

export class EquipmentsComponent extends AppComponentBase {
  @ViewChild('createOrEditEquipmentModal') createOrEditEquipmentModal: CreateOrEditEquipmentModalComponent;
  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('paginator') paginator: Paginator;

  advancedFiltersAreShown = false;
  filterText = '';

  teamGroups: ComboboxItemDto[];

  teamGroupId: number = undefined;
  equipCapacity: boolean = undefined;
  isHeatEquip: boolean = undefined;

  constructor(
    injector: Injector,
    private _http: Http,
    private _equipmentServiceProxy: EquipmentServiceProxy,
    private _notifyService: NotifyService,
    private _fileDownloadService: FileDownloadService,
  ) {
    super(injector);
  }

  getEquipments(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }

    this.primengTableHelper.showLoadingIndicator();

    this._equipmentServiceProxy.getEquipments(
      this.filterText,
      this.equipCapacity,
      this.teamGroupId,
      this.isHeatEquip,
      this.primengTableHelper.getSorting(this.dataTable),
      this.primengTableHelper.getMaxResultCount(this.paginator, event),
      this.primengTableHelper.getSkipCount(this.paginator, event)
    ).subscribe(result => {
      this.primengTableHelper.totalRecordsCount = result.totalCount;
      this.primengTableHelper.records = result.items;
      this.primengTableHelper.hideLoadingIndicator();

    });
  }


  reloadPage(): void {
    this.paginator.changePage(this.paginator.getPage());
  }

  exportToExcel(): void {
    // this._userServiceProxy.getUsersToExcel()
    // .subscribe(result => {
    //     this._fileDownloadService.downloadTempFile(result);
    // });
  }


  deleteEquipment(equipment: EquipmentListDto): void {
    this.message.confirm(
      '确认删除设备(' + equipment.equipName + ')',
      this.l('AreYouSure'),
      (isConfirmed) => {
        if (isConfirmed) {
          this._equipmentServiceProxy.deleteEquipment(equipment.id)
            .subscribe(() => {
              this.reloadPage();
              this.notify.success(this.l('SuccessfullyDeleted'));
            });
        }
      }
    );
  }

  boundSchedule(equipment: EquipmentListDto): void {

  }

  boundMaintenance(equipment: EquipmentListDto): void {

  }
}
